<?php


namespace App\Http\Controllers\Admin;


use App\Exceptions\LevelExistsException;
use App\Models\Level;

class LevelController extends Controller
{
    /*
     * @api get /study-api/a/levels 评测级别
     * @version 0.0.1
     * @group admin
     *
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function index()
    {
        $this->validate($this->request, [
            'pageSize' => "integer|max:100|min:5",
            'page' => "integer|min:1",
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $query = Level::query();
        $total = $query->count();
        $levelsObj = $query->orderBy('id', 'desc')->forPage($page, $pageSize)
            ->get();
        $items = [];
        if ($total) {
            /** @var Level $levelObj */
            foreach ($levelsObj as $levelObj) {
                $items[] = [
                    'id' => $levelObj->getId(),
                    'level' => $levelObj->getLevel(),
                    'wordCount' => $levelObj->getWordCount(),
                    'rate' => $levelObj->getRate()
                ];
            }
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }

    /*
     * @api post /study-api/a/levels 新增评测级别
     * @version 0.0.1
     * @group admin
     *
     * @param body int level 级别
     * @param body int wordCount 单词量
     * @param body int rate 识别率
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'level' => 'required|integer|min:1,max:3',
            'wordCount' => 'required|integer',
            'rate' => 'required|integer|min:1,max:100',
        ]);
        $level = $this->request->input('level');
        $wordCount = $this->request->input('wordCount');
        $rate = $this->request->input('rate');
        if (Level::where('level', $level)->exists()) {
            throw new LevelExistsException();
        }

        /** @var Level $levelObj */
        $levelObj =  new Level();
        $levelObj->setLevel($level);
        $levelObj->setWordCount($wordCount);
        $levelObj->setRate($rate);
        $levelObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api put /study-api/a/levels/{levelId} 编辑评测级别
     * @version 0.0.1
     * @group admin
     *
     * @param path int levelId 级别ID
     * @param body int wordCount 单词量
     * @param body int rate 识别率
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update($levelId)
    {

        $this->request->merge(['id' => $levelId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'wordCount' => 'required|integer',
            'rate' => 'required|integer|min:1,max:100',
        ]);
        $wordCount = $this->request->input('wordCount');
        $rate = $this->request->input('rate');

        /** @var Level $levelObj */
        $levelObj = Level::find($levelId);
        $levelObj->setWordCount($wordCount);
        $levelObj->setRate($rate);
        $levelObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /study-api/a/levels/{levelId} 删除评测级别
     * @version 0.0.1
     * @group admin
     *
     * @param path int templateId 模版ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function destroy($levelId)
    {
        Level::destroy(intval($levelId));
        return $this->sendSuccessJson();
    }
}